<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>第一篇文章 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="import os.path import requestsfrom lxml import etreeif name &#x3D;&#x3D; ‘main‘:    url &#x3D; ‘http:&#x2F;&#x2F;pic.netbian.com&#x2F;4kfengjing&#x2F;&#39;#获取网站地址    headers &#x3D; {        ‘User-Agent’:’Mozilla&#x2F;5.0">
<meta property="og:type" content="article">
<meta property="og:title" content="第一篇文章">
<meta property="og:url" content="http://example.com/2023/10/07/%E7%AC%AC%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="import os.path import requestsfrom lxml import etreeif name &#x3D;&#x3D; ‘main‘:    url &#x3D; ‘http:&#x2F;&#x2F;pic.netbian.com&#x2F;4kfengjing&#x2F;&#39;#获取网站地址    headers &#x3D; {        ‘User-Agent’:’Mozilla&#x2F;5.0">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-10-07T07:04:02.000Z">
<meta property="article:modified_time" content="2023-11-20T03:05:05.571Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-第一篇文章" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2023/10/07/%E7%AC%AC%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0/" class="article-date">
  <time class="dt-published" datetime="2023-10-07T07:04:02.000Z" itemprop="datePublished">2023-10-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      第一篇文章
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>import os.path</p>
<p>import requests<br>from lxml import etree<br>if <strong>name</strong> &#x3D;&#x3D; ‘<strong>main</strong>‘:<br>    url &#x3D; ‘<a target="_blank" rel="noopener" href="http://pic.netbian.com/4kfengjing/'#%E8%8E%B7%E5%8F%96%E7%BD%91%E7%AB%99%E5%9C%B0%E5%9D%80">http://pic.netbian.com/4kfengjing/&#39;#获取网站地址</a><br>    headers &#x3D; {<br>        ‘User-Agent’:’Mozilla&#x2F;5.0 (Macintosh: Intel Mac OS X 10 12 0) AppleWebkit&#x2F;573.36 (KHTML,like Gecko) Chrome&#x2F;72.0.3626.121 Safari&#x2F;537.36’<br>    }<br>    response &#x3D; requests.get(url&#x3D;url,headers&#x3D;headers)#请求网站<br>    page_text &#x3D; response.text</p>
<pre><code>tree = etree.HTML(page_text)
li_list = tree.xpath(&#39;//div[@class=&quot;slist&quot;]/ul/li&#39;)

if not os.path.exists(&#39;./picLibs&#39;):
    os.mkdir(&#39;./picLibs&#39;)

for li in li_list:
    img_src = &#39;http://pic.netbian.com&#39;+li.xpath(&#39;./a/img/@src&#39;)[0]
    img_name = li.xpath(&#39;./a/img/@alt&#39;)[0]+&#39;.jpg&#39;
    #处理中文乱码的解决方案
    img_name = img_name.encode(&#39;iso-8859-1&#39;).decode(&#39;gbk&#39;)
    img_data = requests.get(url=img_src,headers=headers).content
    img_path = &#39;picLibs/&#39;+img_name
    with open(img_path,&#39;wb&#39;) as fp:
        fp.write(img_data)
        print(img_name,&#39;下载成功！！！&#39;)
</code></pre>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2023/10/07/%E7%AC%AC%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0/" data-id="clnfowlou0000ysvb3e531u08" data-title="第一篇文章" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2023/11/19/%E7%88%AC%E8%99%AB%E6%97%A5%E8%AE%B0/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          爬虫日记
        
      </div>
    </a>
  
  
    <a href="/2023/10/07/hello-world/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Hello World</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/11/">November 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/10/">October 2023</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/11/19/%E7%88%AC%E8%99%AB%E6%97%A5%E8%AE%B0/">爬虫日记</a>
          </li>
        
          <li>
            <a href="/2023/10/07/%E7%AC%AC%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0/">第一篇文章</a>
          </li>
        
          <li>
            <a href="/2023/10/07/hello-world/">Hello World</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>